import com.qi.dao.UserMapper;
import com.qi.pojo.User;
import com.qi.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

/**
 * @author zjq
 * @date 2020/3/11 15:46
 */
public class UserMapperText {
    @Test
    public void test() {
        //第一步，获得sqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        try {
            //方式1.getMapper
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            List<User> userList = userMapper.getUserList();

            //方式二：

            for (User user : userList) {
                System.out.println(user);
            }
            //System.out.println(userList);

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            //关闭sqlSession
            sqlSession.close();
        }

    }

    @Test
    public void getUserId() {
        //第一步，获得sqlSession对象
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        System.out.println(mapper.getUserById(1));

        sqlSession.close();

    }


    //@Test
//        public void UpdateUser(){
//            SqlSession sqlSession = MyBatisUtils.getSqlSession();
//            UserDao mapper = sqlSession.getMapper(UserDao.class);
//            mapper.updateUser(new User(1,55,55));
//            sqlSession.commit();
//            sqlSession.close();
//        }
    @Test
    public void deleteUser() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }
        mapper.deleteUser(3);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void findUser() {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        List<User> userList = mapper.getUserList();
        for (User user : userList) {
            System.out.println(user);
        }

        sqlSession.close();
    }

}
